import { Component, OnInit,Input,OnChanges,SimpleChanges,DoCheck } from '@angular/core';

@Component({
  selector: 'app-cchange',
  templateUrl: './cchange.component.html',
  styleUrls: ['./cchange.component.css']
})
export class CchangeComponent implements OnInit,OnChanges,DoCheck {

  @Input()
  greeting:string;

  @Input()
  user:{name:string};

  message:string = "初始化消息"

  oldUsername: string;//存储之前的
  changeDetected:boolean = false ;//检测是否发生变化的。
  noChangeCount:number;
  constructor() { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges):void {
    console.log(JSON.stringify(changes,null ,2))
  }

  ngDoCheck() {
    if(this.user.name !== this.oldUsername) {
      this.changeDetected = true;
      console.log("DoCheck的user.name从-"+this.oldUsername+"-变成-"+this.user.name);
      this.oldUsername = this.user.name;
    }
    if(this.changeDetected) {
      this.noChangeCount = 0;
    }else {
      this.noChangeCount = this.noChangeCount + 1;
      console.log("user.name没有发生变化时，ngDoCheck方法已经被调用"+ this.noChangeCount + "次")
    }

    this.changeDetected = false;
  }

}
